import axios from 'axios'

// 查询一级分类
export function fetchCategoryLevel1(params: { name?: string }) {
  return axios.get('/api/safety/category/list', {
    params: {
      level: 1,
      ...params
    }
  })
}

// 新增一级分类
export function addCategory(data: any) {
  return axios.post('/api/safety/category/add', data)
}

// 编辑一级分类
export function updateCategory(data: any) {
  return axios.post('/api/safety/category/update', data)
}

// 删除一级分类
export function deleteCategory(id: number) {
  return axios.post('/api/safety/category/delete', { id })
}

// 查询二级分类
export function fetchCategoryLevel2(params: any) {
  // 保证parentId为数字或undefined
  const query = { ...params };
  if (query.parentId === '' || query.parentId === undefined) {
    delete query.parentId;
  } else {
    query.parentId = Number(query.parentId);
  }
  return axios.get('/api/safety/category/list', {
    params: {
      level: 2,
      ...query
    }
  })
}

// 新增二级分类
export function addCategoryLevel2(data: any) {
  return axios.post('/api/safety/category/sub/add', data)
}

// 编辑二级分类
export function updateCategoryLevel2(data: any) {
  return axios.post('/api/safety/category/sub/update', data)
}

// 删除二级分类
export function deleteCategoryLevel2(id: number) {
  return axios.post('/api/safety/category/sub/delete', { id })
}

// 你可以继续添加其他接口方法 